﻿
namespace Glimpse.Elmah.Responder.Resources
{
	[ElmahResource]
	public abstract class ElmahEmbeddedResource : IElmahResource
	{
		private readonly string _identity;
		private readonly string _type;
		private readonly string _name;

		protected ElmahEmbeddedResource(string identity, string type, string name)
		{
			_identity = identity;
			_type = type;
			_name = name;
		}

		public string Identity { get { return _identity; } }
		public string Type { get { return _type; } }
		
		public byte[] Data
		{
			get
			{
				var assembly = GetType().Assembly;
				using (var stream = assembly.GetManifestResourceStream(_name))
				{
					if (stream == null)
						return null;

					var buffer = new byte[stream.Length];
					stream.Read(buffer, 0, (int)stream.Length);

					return buffer;
				}
			}
		}
	}
}